package org.kia.activity;

import java.io.File;
import java.util.Random;

import org.kia.R;
import org.kia.data.Mocker;
import org.kia.data.model.AccessibilityItem;
import org.kia.data.model.ItemModel;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class ProfileActivity extends Activity {

	public static final int imgRequestCode = 1000;

	private LayoutInflater mInflater;
	private Bitmap mBitmap; 
	private ItemModel mItem;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.insert);
        mInflater = LayoutInflater.from(this);
        mItem = Mocker.getItem();
        fillItemDetails();
        ListView list = (ListView) findViewById(android.R.id.list);
        list.setAdapter(new InsertDetailAdapter());
        
    }
    
    private void fillItemDetails() {
    	EditText mTitleView = (EditText) findViewById(R.id.name);
    	mTitleView.setText(mItem.getName());
    }
    
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) 
    {
    	switch (requestCode) 
    	{
        	case imgRequestCode:
        		Uri u = intent.getData(); 
        		ImageView myView = (ImageView)findViewById(R.id.imageView1);
        		mBitmap = BitmapFactory.decodeResource(getResources(), R.id.imageView1);
//        		m_Bitmap = BitmapFactory.decodeFile(PictureCaptureActivity.FILE_DIR + PictureCaptureActivity.PHOTO_NUMBER + ".jpg");
        		// Take a picture with the camera, save it to internal memory
        		myView.setImageBitmap(this.mBitmap);
        		
        		myView.setImageURI(u);
    	}

    }

    
    private class InsertDetailAdapter extends BaseAdapter {
    	public static final int MAX_RANDOM_NAME = 63; 
    	public static final int MIN_RANDOM_NAME = 0;
    	public static final String FILE_LAST_DIR = "/KnowInAdvance/";
    	public int PHOTO_NUMBER = -1;
    	private final int IMAGE_CAPTURE = 0;
    	private Uri imageUri;
    	public String FILE_DIR = "";
    	public String FILE_NAME = "";
    	public String FILE_PATH = "";
    	
		public int getCount() {
			return mItem.getAccessibilityParams().size();
		}

		public Object getItem(int position) {
			return mItem.getAccessibilityParams().get(position);
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) 
		{
			
			View line = mInflater.inflate(R.layout.access_line, null);
			AccessibilityItem item = (AccessibilityItem) getItem(position);
			TextView nameView = (TextView) line.findViewById(R.id.name);
			nameView.setText(item.getName());
			
			line.findViewById(R.id.imageButton1).setOnClickListener(new OnClickListener() 
			{
				
				@Override
				public void onClick(View v) 
				{
					//randomize filename
					Random random = new Random();
					
					// ----Randomize photo number name---- 
					PHOTO_NUMBER = random.nextInt(MAX_RANDOM_NAME - MIN_RANDOM_NAME) + MIN_RANDOM_NAME;
					FILE_NAME = PHOTO_NUMBER + ".jpg";
					FILE_DIR = Environment.getExternalStorageDirectory() + FILE_LAST_DIR;
					FILE_PATH = FILE_DIR + FILE_NAME;
					
					File imageFile = new File(FILE_PATH);
					
					ContentValues values = new ContentValues();
					values.put(MediaStore.Images.Media.TITLE, FILE_PATH);
					values.put(MediaStore.Images.Media.DESCRIPTION, "Image capture by camera");
					values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
					imageUri = Uri.fromFile(imageFile);
					
					Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
					intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
					intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
					
					startActivityForResult(intent, imgRequestCode);		
				}
			}
			);
			return line;
		}
    	
    }
}